package com.hepinghospital.user.util;

import android.content.Context;
import android.os.Environment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hepinghospital.user.R;
import com.rest.business.RestProxy;
import com.rest.observer.FileDownLoadObserver;

import java.io.File;
import java.util.Objects;


/**
 * Created by Administrator on 2018/1/2.
 */

public class ImageLoaderHelper {
    private static class Instance {
        static ImageLoaderHelper instance = new ImageLoaderHelper();
    }

    public static ImageLoaderHelper getInstance() {
        return Instance.instance;
    }

    private ImageLoaderHelper() {
    }

    public void GlideImageLoader(Context context, String url, ImageView imageView, int defaultPic) {
        Glide.with(context.getApplicationContext()).load(url).format(DecodeFormat.PREFER_ARGB_8888).placeholder(defaultPic).into(imageView);
    }

    public void GlideImageLoader(Context context, String url, ImageView imageView) {
        Glide.with(context.getApplicationContext()).load(url).format(DecodeFormat.PREFER_ARGB_8888).into(imageView);
    }


    public void GlideCircleImageLoader(final Context context, String url, final ImageView imageView) {
        Glide.with(context.getApplicationContext())
                .load(url)
                .placeholder(R.mipmap.img_default)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .dontAnimate()
                .into(imageView);
    }

    /**
     * 加载圆形图片
     */
    public void setCirclePicture(Context context, ImageView view, String url) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
//                       .placeholder(R.mipmap.ic_launcher_round) //预加载图片
                .error(R.drawable.ic_launcher_foreground) //加载失败图片
                .priority(Priority.HIGH) //优先级
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //缓存
                .transform(new CircleCrop()); //圆形

        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)//跳过内存缓存。
                .dontAnimate()
//                .diskCacheStrategy( DiskCacheStrategy.RESOURCE )//全部使用磁盘缓存
                .error(R.mipmap.icon)
                .apply(options)
                .into(view);

    }


    public void GlideImageLoaderByStream(final Context context, final String url, final ImageView imageView) {
        File pictureDirOri = new File(Objects.requireNonNull(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)).getAbsolutePath() + "/OriPicture");
        String imgNameOri = "HomePic_" + System.currentTimeMillis();

        RestProxy.getInstance().downloadFile(url, pictureDirOri.getAbsolutePath(), imgNameOri + ".jpg", new FileDownLoadObserver<File>() {
            @Override
            public void onDownLoadSuccess(File file) {
                Glide.with(context.getApplicationContext()).load(file).into(imageView);
            }

            @Override
            public void onDownLoadFail(Throwable throwable) {

            }

            @Override
            public void onProgress(int progress, long total) {

            }
        });
    }


}
